import { get_filter_list, get_id_filter, set_filter, update_filter, delete_filter } from '@/api/report_filter'

const filter = {
  namespaced: true,
  state: {
    filterList: []
  },
  
  mutations: {
    SET_FILTER_LIST: (state, list) => {
      state.filterList = list
    }
  },
  
  actions: {
    // 获取筛选条件列表
    getFilterData ({ commit }, id) {
      return new Promise((resolve, reject) => {
        get_filter_list(id)
          .then(res => {
            if (res.code == 0) {
              commit('SET_FILTER_LIST', res.data)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 新建
    set_new_filter ({}, form) {
      return new Promise((resolve, reject) => {
        set_filter({ ...form })
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 修改
    updateFilter ({}, form) {
      return new Promise((resolve, reject) => {
        update_filter({ ...form })
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 获取修改项
    get_update_id ({}, id) {
      return new Promise((resolve, reject) => {
        get_id_filter(id)
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 删除筛选条件
    deleteFilter ({ commit }, id) {
      return new Promise((resolve, reject) => {
        delete_filter(id)
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    }
  },

  getters: {

  }
}

export default filter